/**
 * Copyright (c) 2010 - 2012, Sina Inc. All rights reserved.
 * @fileoverview    创建专题
 * @author bowen liu
 * @email liubowen082@163.com
 * @version 1.0
 */

//引用配置文件
//$require('cpt.LJSDefine');
//$require('cpt.collectionTop');

$require('cpt.LJSDefine');
$require('cpt.modDefineValue');
$require('form.cFormTitle');
$require('form.cFormTextarea');
$require('cpt.uploadPic');
$require('form.cFormselect');
$require('form.cFormCheckbox');
$require('cpt.createProList');
$require('form.cFormNum');
$require('mod.cAlert');
$require('mod.modTemplate');
LJS.define('page.createPro',function(ljs){
    
	$('#postForm')[0].reset();

	/*var upload = new ljs.cpt.uploadPic({
					   uploadBox : a
					   });
	$(upload).on('uploadSuc',function(evt,data){
				$('[node_type="imgId"]' ,a).val(data.content[0].id)
				$('[node_type="imgSrc"]',a).attr('src',data.content[0].url)
					  })
	$(upload).on('uploadErr',function(){
								
									  })
	uploads.push(upload);*/
	
	var title = new ljs.form.cFormTitle({
			'el': $('[form_type="title"]')[0],
			'require': ['focus', 'blur']
		});
	var select1 = new ljs.form.cFormSelect({
               el:  $('[form_type="select1"]')[0]
           });
	
	var select2 = new ljs.form.cFormSelect({
               el:  $('[form_type="select2"]')[0]
           });
	var colors = new ljs.form.cFormCheckbox({
										el : $('#colors')[0]
										})
	var area = new ljs.form.cFormTextarea({
							'el': $('[form_type="textarea"]')[0],
							'arrow':'down',
							'require': ['focus', 'blur']
						})
	var select3 = new ljs.form.cFormSelect({
               el:  $('[form_type="select3"]')[0]
           });
    $('[form_type="select3"]').on('change',function(){
		tower.setValue('')
		$('#towerId').val('')
	})
	//form.cFormNum
	var num = new ljs.form.cFormNum({
               el:  $('[form_type="num"]')[0],
			   'require': ['focus', 'blur']
           });
	var tower = new ljs.form.cFormTitle({
			'el': $('[form_type="tower"]')[0],
			'require': ['focus', 'blur']
		});
	$('[form_type="tower"]').on('blur',function(){
		if($(this).attr('loading')=='true') return;
		$(this).attr('loading','true')
		isLoading = true;
		var that = this;
			if(tower.check()){
				
				var val = tower.getValue();
				if($.trim(val)=='') {
					$(that).attr('loading','false')
					isLoading = false
					return
				}
				$.ajax({
				  type: "get",
				  url: "/cases/ajax_build",
				  //url:'checkPro.js',
				  dataType: "json",
				  data:{bulid_name:val,city_id:select3.getValue()},
				  cache:false,
				  success:function(json){
				  	  
					  if(json.status!='100000'){
					  	    $(that).attr('loading','false')
							ljs.mod.cAlert({txt:json.msg})
							//$(that).val('');
							$('#towerId').val('');
						  	return;
					  }else if(json.status=='100003'){
					  	
					  	window.location.href = json.content.url;
					  	return 
					  }
					   var tpl ='<li><a href="javascript:void(0)" id="#{buildid}" buildname="#{buildname}">#{buildname}</a></li>'
						//var json = {"status":"100000",content:[{"buildid":"3543","buildname":"\u4e2d\u5357\u6e7e"},{"buildid":"3467","buildname":"\u4e2d\u6602\u00b7\u9091\u4e0a\u539f\u8457"},{"buildid":"3354","buildname":"\u4e2d\u79d1\u7d2b\u5cf0"},{"buildid":"3347","buildname":"\u4e2d\u5efa\u00b7\u56fd\u9645\u57ce"},{"buildid":"3341","buildname":"\u4e2d\u6d77\u5fa1\u946b\u9601"},{"buildid":"3323","buildname":"\u4e2d\u56fd\u94c1\u5efa\u56fd\u9645\u82b1\u56ed"},{"buildid":"3275","buildname":"\u4e2d\u7cae\u4e07\u79d1\u957f\u9633\u534a\u5c9b\u7d2b\u4e91\u5ead"},{"buildid":"3268","buildname":"\u5317\u4eac\u8bfa\u5fb7\u4e2d\u5fc3"},{"buildid":"3263","buildname":"\u666f\u5c71\u8d22\u5bcc\u4e2d\u5fc3"},{"buildid":"3207","buildname":"\u4e2d\u5965\u5e7f\u957f\u9996\u7ad9"}]}
						var arr = [];
						$.each(json.content,function(i,a){
							arr.push(ljs.mod.modTemplate(tpl,{buildname:a.buildname,buildid:a.buildid}))
						})
						var lay = document.createElement('ul');
						
						$(lay).addClass('inputList').css({background:'#FFF',zIndex:'100',width:'250px',height:'200px',overflow:'auto',position: 'absolute',left:$(that).offset().left,top:$(that).offset().top+30})
						lay.innerHTML = arr.join('');
						
						$(document.body).append(lay);
						$(lay).on('click','a',function(){
							var id = $(this).attr('id');
							var buildname = $(this).attr('buildname');
							$(that).val(buildname)
							$('#towerId').val(id);
							$(that).attr('loading','false')
							$(lay).remove();
						})
				  }
				});
				
				
				}else{
					$(this).attr('loading','false')
				}
					
				
				})

	var _createProList = ljs.cpt.createProList;
    _createProList.init('#uploadImg');
    _createProList.createList(JAJU.proList)

	
	
	$('[form_submit_btn="submitBtn"]').on('click',function(){
		if($('[form_type="tower"]').attr('loading')=='true') {
			
			return
		};
		
						$('#createError').hide();
						if(!title.check()){
							$('#createError').show();
							title.showError();
							return
							}
						if(!select1.check()){
							$('#createError').show();
							select1.showError();
							return
							}
						if(!select2.check()){
							$('#createError').show();
							select2.showError();
							return
							}
						if(!colors.check()){
							$('#createError').show();
							colors.showError();
							return
							}
						if(!area.check()){
							$('#createError').show();
							area.showError();
							return
							}else{
								area.setValue($.trim(area.getValue()))
							}
						if(select3.check()){
							if(!tower.check()){
								$('#createError').show();
							    tower.showError();
							    return
							}
						}
						// if(!select3.check()){
							// $('#createError').show();
							// select3.showError();
							// return
							// }
					    if(tower.getValue()!=''){
							if(select3.check()&&select3.getValue()!=''){
								if($('#towerId').val()==''&&$('#towerId').val()==0){
									$('#createError').show();
									ljs.mod.cAlert({txt:'城市与楼盘不匹配'})
									return
								}
							}else{
								$('#createError').show();
								select3.showError();
								return
							}
							
						}
						if(tower.getValue()==''){
							$('#towerId').val('')
							}
						if(!num.check()){
							$('#createError').show();
							num.showError();
							return
							}
						var _data = ljs.cpt.createProList.getData();
						if(_data.length==0){
							$('#createError').show();
							return
							}
						var str = $.jsonToStr(_data);
						$('#proJsonStr').val(str);
						
		                
						var status=$(this).attr('status');
						$('#status').val(status)
						$('#postForm').submit();
						})
})

